/**
 * 作者:侯鑫林
 * 时间:19/3/14 19:14
 * 
 */
Page({
  data: {
    animationData: '',
    screenHeight: 0,
    isAllSelect: false,
    totalMoney: 0,//总共价格
    nowFoodTypeSelectIndex: 0,//当前选择索引
    buyTotal: 0,//购买的个数
    buyList: [],
    cartVisible: false,
    buyGoodList: [],
    // 商品详情介绍
    AllFoodList: [{
      "foodTitle":"方便面",
      "foodList":[{
        "goodName":"北京方便面",
        "goodPerview":"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1552571277995&di=6563243fc5ca6853c3c61222c18c17e9&imgtype=0&src=http%3A%2F%2Fm.360buyimg.com%2Fn12%2Fg14%2FM04%2F1B%2F1B%2FrBEhVVMz6xYIAAAAAATdPG8Fw8YAAK7dQL66YQABN1U780.jpg%2521q70.jpg",
        "goodSales":"23",
        "goodPrice":"1",
        "buyNum":0
      }]
    },
      {
        "foodTitle": "辣条",
        "foodList": [{
          "goodName": "卫龙 ",
          "goodPerview": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1552571694581&di=f6994d49aafd1e4ed4a37047c46270c9&imgtype=0&src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20170303%2F9f5a49cc64c04208801413ee15368e5f_th.jpg",
          "goodSales": "23",
          "goodPrice": "1",
          "buyNum": 0
        }, {
            "goodName": "笑笑",
            "goodPerview": "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2148271491,749548308&fm=26&gp=0.jpg",
            "goodSales": "23",
            "goodPrice": "1",
            "buyNum": 0
          }]
      }
    ]

  },
  onLoad: function (options) {
    var that = this;
    this.setData({
      screenHeight: wx.getSystemInfoSync().screenHeight
    })
  },
  changeTypeSelected: function (e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    this.setData({ nowFoodTypeSelectIndex: index })
  },

  // 添加选择的食物数量
  funFoodAdd: function (e) {
    this.calculationMoney;
    var foodIndex = e.currentTarget.dataset.food_index
    var typeIndex = e.currentTarget.dataset.type_index

    var nowNum = this.data.AllFoodList[typeIndex].foodList[foodIndex].buyNum
    nowNum = nowNum + 1
    var tempBuyTotal = this.data.buyTotal;
    this.data.AllFoodList[typeIndex].foodList[foodIndex].buyNum = nowNum
    this.setData({
      AllFoodList: this.data.AllFoodList, buyTotal: tempBuyTotal + 1
    })
    this.calculationMoney();

  },
  //减去食物
  funFoodReduce: function (e) {
    var foodIndex = e.currentTarget.dataset.food_index
    var typeIndex = e.currentTarget.dataset.type_index
    var nowNum = this.data.AllFoodList[typeIndex].foodList[foodIndex].buyNum
    if (nowNum == 0) {
      return;
    }
    nowNum = nowNum - 1
    var tempBuyTotal = this.data.buyTotal;

    this.data.AllFoodList[typeIndex].foodList[foodIndex].buyNum = nowNum
    this.setData({
      AllFoodList: this.data.AllFoodList, buyTotal: tempBuyTotal - 1
    })
    this.calculationMoney();
  },
  //计算总价
  calculationMoney: function () {
    var tempMoney = 0;
    var parent = this.data.AllFoodList.length;
    for (var i = 0; i < parent; i++) {
      for (var j = 0; j < this.data.AllFoodList[i].foodList.length; j++) {
        tempMoney = tempMoney + (this.data.AllFoodList[i].foodList[j].buyNum * this.data.AllFoodList[i].foodList[j].goodPrice)
      }

    }
    this.setData({
      totalMoney: tempMoney
    })

  },
  //显示购物车
  showCartDialog: function () {
    this.setData({
      cartVisible: !this.data.cartVisible
    })
  },
  //清空购物车
  funCartEmpty: function () {
    var temp = this.data.AllFoodList;
    for (var i = 0; i < temp.length; i++) {
      for (var j = 0; j < temp[i].foodList.length; j++) {
        temp[i].foodList[j].buyNum = 0;
      }
    }
    this.calculationMoney();
    this.setData({
      AllFoodList: temp, buyTotal: 0
    })

  },
  buyGoodFunction:function(){
  },
  onShow: function () {
  
  },
});